package ae.pcfc.etks.online.web.controller.register;

/* $Id: PasswordController 
 * Copyright (C) 2011, Trakhees
 *  
 *  History
 *
 * Date  			Author 					Remarks
 * -------------------------------------------------------------
 * April 10, 2011 	Neha_Chauhan 	        Created
 *
 */

import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.delegate.UserDelegate;
import ae.pcfc.etks.online.common.vo.ClientEmailVO;
import ae.pcfc.etks.online.common.vo.PasswordResetVO;
import ae.pcfc.etks.online.common.vo.UserVO;
import ae.pcfc.etks.online.web.ConstantsAdmin;
import ae.pcfc.etks.online.web.PageType;
import ae.pcfc.etks.online.web.ViewPath;
import ae.pcfc.etks.online.web.ViewUtils;
import ae.pcfc.etks.online.web.controller.BaseController;
import ae.pcfc.etks.online.web.model.ClientPasswordLogin;
import ae.pcfc.etks.online.web.util.SecurityUtils;

/**
 * Controller that handles all {@link PasswordController}-related requests,
 * i.e.: all requests for /client/register/password/*
 * 
 * @author Neha Chauhan
 */

@Controller
@RequestMapping(ViewPath.FORGOT_PASSWORD)
public class PasswordController extends BaseController{

	private UserDelegate userDelegate;

	@Resource(name = "userDelegate")
	public void setUserDelegate(UserDelegate userDelegate) {
		this.userDelegate = userDelegate;
	}

	/**
	 * Custom handler for displaying the form, i.e.: GET requests for
	 * /client/register/password
	 * 
	 * @see WEB-INF/jsp/client/register/password/form.jsp
	 * @see WEB-INF/jsp/client/register/password/show.jsp
	 */

	@RequestMapping(value = "/form.htm", method = RequestMethod.GET)
	public void form(@ModelAttribute(value = ConstantsAdmin.CLIENT_REGISTER_PASSWORD_MODEL_KEY) ClientPasswordLogin command) {

	}

	@RequestMapping(value = "/show.htm", method = RequestMethod.GET)
	public void show() {

	}

	/**
	 * Custom handler for displaying the form, i.e.:POST requests for
	 * /client/register/password/form.jsp , /client/register/password/show.jsp
	 * 
	 * @see #form(ClientPasswordLogin)
	 * @see WEB-INF/jsp/client/register/password/form.jsp
	 *      WEB-INF/jsp/client/register/password/show.jsp
	 */

	@RequestMapping(method = RequestMethod.POST, value = "/form.htm")
	public String find(
			HttpServletRequest request,
			Locale locale,
			SessionStatus status,
			@ModelAttribute(value = ConstantsAdmin.CLIENT_REGISTER_PASSWORD_MODEL_KEY) @Valid ClientPasswordLogin clientPasswordRegister,
			BindingResult errors) {

		if (errors.hasErrors()) {
			for (Object object : errors.getAllErrors()) {
				if (object instanceof FieldError) {
					FieldError fieldError = (FieldError) object;

				}

				if (object instanceof ObjectError) {
					ObjectError objectError = (ObjectError) object;

				}
			}

			return ViewUtils.DEFAULT_VIEW;

		} else {
//			UserVO userVO = this.userDelegate.findUserByEmail(clientPasswordRegister.getStrEmail());
			UserVO userVO = this.userDelegate.findUserByUserName(clientPasswordRegister.getStrUserName());

			if (userVO != null && userVO.getlUserId() != null) {
				String strPassword = RandomStringUtils.randomAlphanumeric(ConstantsCommon.PASSWORD_RESET_DEFAULT_LENGTH);

				PasswordResetVO passwordResetVO = new PasswordResetVO();

				//passwordResetVO.setStrEmail(userVO.getStrEmail());
				passwordResetVO.setlUserId(userVO.getlUserId());
				passwordResetVO.setStrPassword(SecurityUtils.hashPassword(strPassword));

				// Reseting the password to a default generated password
				this.userDelegate.resetPassword(passwordResetVO);

				ClientEmailVO clientEmailVO = new ClientEmailVO();

				clientEmailVO.setStrEmailAddress(userVO.getStrEmail());
				clientEmailVO.setStrEmailSubject(getMessageSource().getMessage("msg.forgot.password.subject", null, locale));
				clientEmailVO.setStrEmailBody(getMessageSource().getMessage("msg.forgot.password.body",
						new String[] { userVO.getStrUsrName(), strPassword }, locale));
				this.userDelegate.getUserNameAndPassword(clientEmailVO);
				return ViewUtils.getRedirect(ViewPath.FORGOT_PASSWORD, PageType.SHOW);

			} else {

				errors.reject("error.client.username.not.found", null, null);
			}
			return ViewUtils.DEFAULT_VIEW;

		}
	}

}
